<?php
namespace Admin\Controller;
use Common\Controller\BackendController;
class RefundController extends BackendController{
	public function _initialize() {
        parent::_initialize();
        $this->_mod = D('Refund');
    }
    /**
     * 申请保证金退款列表
     */
    public function index(){
        $this->_name = 'Refund';
        $db_pre = C('DB_PREFIX');
        $key_type   = I('request.key_type',0,'intval');
        $key        = I('request.key','','trim');
        $join = array();
        //$join[] = 'left join '.$db_pre."members as m on ".$this_t.".uid=m.uid";
        //$this->field = $this_t.'.*,m.username,m.mobile,m.email as memail';
        if ($key && $key_type>0){
            switch($key_type){
                case 1:
                    $where['id']=array('eq',$key);break;
                case 2:
                    $where['jobs_name']=array('like','%'.$key.'%');break;
                case 3:
                    $where['companyname']=array('like','%'.$key.'%');break;
            }
        }else{
            if($settr=I('get.settr',0,'intval')){
                $where['addtime']=array('gt',strtotime("-".$settr." day"));
            }
            $status=I('get.status',0,'intval');
            if(!empty($status)){
                $where['status']=array('eq',$status);
            }
        }

        $this->where = $where;
        $this->order = 'addtime desc';

        //$this->join = $join;
        $this->assign('count',parent::_pending('CompanyProfile',array('audit'=>2)));
        $setmeal = D('Setmeal')->get_setmeal_cache();
        $this->assign('setmeal',$setmeal);
        parent::index();
    }


    /**
     * 审核保证金状态
     */
    public function edit(){
        $id = I('request.id',0,'intval');
        if(!$id){
            $this->returnMsg(0,'你没有选择申请退款记录！');
        }
        $refund = D('Refund')->where(array('id'=>$id))->find();
        if(!$refund) $this->returnMsg(0, '退款记录不存在！');
        if(!IS_POST)
        {
            $this->assign('info', $refund);
            $this->display();
        }
        else //保存企业信息
        {
            $setsqlarr['id']=$id;
            $setsqlarr['remarks']= I('post.remarks','','trim,badword');
            $setsqlarr['status'] = I('post.status',0,'intval');
            $setsqlarr['uptime'] = time();
            if($setsqlarr['status'] < 2){
                $this->returnMsg(0,'审核状态必须为通过或拒绝！');
            }

            $rst = D('Refund')->save($setsqlarr);
            if($rst){
                if($setsqlarr['status']==2){//审核通过，然后退款
                    if($refund['payment']=='wxpay'){
                        //微信退款
                        $order = M("Order")->where(['id'=>$refund['order_pay_id']])->find();
                        if(!$order){
                            $setsqlarr['status']    = 1;
                            D('Refund')->save($setsqlarr);
                            $this->returnMsg(0, '退款订单信息不存在');
                        }
                        $oid = "W-" . date('ymd', time()) . "-" . date('His', time()) . "-" . mt_rand(1000, 9999);
                        $order['r_oid'] = !empty($order['r_oid']) ? $order['r_oid'] : $oid;
                        //$refundPay  = refund_pay($order['oid'], $order['r_oid'], $order['pay_amount'], $refund['money'], 'refundPay');
                        $refundPay  = refund_pay($order['oid'], $order['r_oid'], 0.01, 0.01, 'refundPay');
                        if($refundPay['status']==1){//错误之前的数据返回
                            M("Order")->where(['id'=>$refund['order_pay_id']])->save(['r_oid'=> $order['r_oid'], 'refund_status'=>2, 'refund_status_time'=> time()]);
                            $this->returnMsg(0, $refundPay['msg']);
                        }

                    }elseif($refund['payment']=='gcb'){
                        //工程比支付
                        M('MembersSetgcb')->where(['uid'=> $refund['uid']])->setDec('use_num', $refund['money']);
                        M("Order")->where(['id'=>$refund['order_pay_id']])->save(['refund_status'=>2, 'refund_status_time'=> time()]);
                    }else{
                        //线下支付
                        M("Order")->where(['id'=>$refund['order_pay_id']])->save(['refund_status'=>2, 'refund_status_time'=> time()]);
                        $this->returnMsg(1, '审核成功，请线下联系客户进行退款！');
                    }

                    //写入退款日志 start++++++++++++++++++++++++++++
                    $data   = [
                        'use_type'  => $refund['payment'],
                        'order_type'=> 'tuikuan',
                        'price'     => $refund['money'],
                        'oid'       => $refund['order_pay_id'],
                        'job_id'    => $refund['jobs_id'],
                        'uid'       => $refund['uid'],
                        'desc'      => '保证金退款',
                        'add_time'  => date('Y-m-d H:i:s', time()),
                        'add_type'  => 3
                    ];
                    M('MemberUseLog')->add($data);
                    //end+++++++++++++++++++++++++++++++++++++++++
                }elseif($setsqlarr['status']==3){
                    M("Order")->where(['id'=>$refund['order_pay_id']])->save(['refund_status'=>3, 'refund_status_time'=> time()]);
                    $this->returnMsg(0, '拒绝退款成功！');
                }
                $this->returnMsg(1, '退款成功！');
            }else {
                $this->returnMsg(0, $rst['error']);
            }
        }
    }

}
?>